package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Page;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service("sysUserServiceImpl")
public class SysUserServiceImpl implements SysUserService {
    private static final Logger looger= LoggerFactory.getLogger(SysUserServiceImpl.class);
    //定义Mapper
    @Resource
    private SysUserMapper sysUserMapper;


    @Override
    public List<SysUser> getSysUserList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }

    @Transactional
    @Override
    public int savenBatch(List<SysUser> sysUserList) {
        int rows=0;
        for (int i = 0; i < sysUserList.size(); i++) {
            //拿出每一条用户数据插入
            SysUser sysUser=sysUserList.get(i);
            if (i==1){
                //插入第二条数据时 模拟数据库异常
                throw new RuntimeException("模拟数据库异常");
            }
            rows+=sysUserMapper.insert(sysUser);
        }
        return rows;
    }

    @Override
    public SysUser login(String account, String password) {
        SysUser loginUser=sysUserMapper.getByAccount( account);
        //如果账号存在且密码相等 则返回查到的登录用户信息
        if (loginUser!=null && loginUser.getPassword().equals(password)){
            return loginUser;
        }
        return null;
    }

    @Override
    public Page<SysUser> findPage(SysUser entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<SysUser> page=null;
        try{
            page=new Page<SysUser>();
            page.setPageSize(pageSize);
            System.out.println(pageSize);
            //查询总页数,计算总页数
            Long count= sysUserMapper.count(entity);
            page.setTotalPageCount( count.intValue());
            page.setPageNo(pageNo);
            //查询分页数据
             List<SysUser> data=sysUserMapper.getPage(entity,page.offset(),page.getPageSize());
             page.setData(data);
        }catch (Exception e){
            looger.error("执行SysUserServiceImpl.findPage(SysUser,Integer,Integer)方法出现异常.", e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(SysUser sysUser) throws Exception {
        try {
            if (sysUser.getId()==null){
                //无主键 新增
                return sysUserMapper.insert(sysUser)>0;
            }else {
                //存在主键 修改
                return sysUserMapper.update(sysUser)>0;
            }
        }catch (Exception e){
            looger.error("执行SysUserServiceImpl.saveOrUpdate(SysUser)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public SysUser findById(Long id) throws Exception {
        try{
            return sysUserMapper.getById(id);
        }catch (Exception e){
            looger.error("执行SysUserServiceImpl.findById(Integer)方法出现异常.", e);
            throw e;
        }
    }
}